// 测试地址：http://127.0.0.1:5099/api/torasa/sendRasa
// {
//     "url":"http://39.99.139.84:5005/webhooks/rest/webhook",
//     "type":"post",
//     "data":{"sender":"111","message":"你好"}
// }
function sendRasa(data) {


    console.log("打印请求数据" + data.url)


    //解析请求数据
    var url = data.url; //请求地址
    var type = data.type; //请求类型
    var arg = data.data; //请求数据
    if (url === "") {
        return json.data;
    }
    if ("get" === (type.toLowerCase())) {
        var json = sendGet(url, arg);
        console.log("打印请求结果" + json.data)
        console.log(json)
        // return json.data;
    }
    if ("post" === (type.toLowerCase())) {
        //请求数据形式为application/json

        var json = sendPostJson(url, arg);
        console.log("打印请求结果" + json.data)
        console.log(json)
        // return json.data;
    }
    return json.data;

}

function sendGet(url, data) {
    console.log("开始解析json")
    console.log(data)

    var arg = ""
    for (var key in data) {

        // 变成 key = value形式
        console.log(key)
        console.log(data[key])
        var s = key + "=" + data[key];
        arg += s;
        arg += "&";

    }

    var json = Process("xiang.network.Get", url + "?" + arg)
    console.log("get请求内容1" + url)
    console.log("get请求内容2" + arg)
    return json;
}


function sendPostJson(url, data) {
    console.log(data)
    var json = Process("xiang.network.PostJSON", "http://39.99.139.84:5005/webhooks/rest/webhook", {"sender":"111","message":"介绍一下你们公司"})
    // var json = Process("xiang.network.PostJSON", url, data)
    console.log("post请求内容1" + url)
    console.log("post请求内容2" + data)
    return json;
}


//对rasa发送Get请求
function sendPostJSONTest(sender, message) {
    console.log(sender)
    console.log(message)
    var json = Process("xiang.network.PostJSON", "http://39.99.139.84:5005/webhooks/rest/webhook",
        {"sender": sender, "message": message})
    console.log(json)
    if (json.status == 200) {
        return json.data;
    }
    return "请求异常";
    //    var json = Process("xiang.network.Post", "http://39.99.139.84:5005/webhooks/rest/webhook",
    //         'yaml信息')
}

function T(){
    var s='{\n' +
        '  "recipe": "default.v1",\n' +
        '  "language": "en",\n' +
        '  "pipeline": null,\n' +
        '  "policies": null,\n' +
        '  "version": "3.0",\n' +
        '  "intents": [\n' +
        '    "welcome"\n' +
        '  ],\n' +
        '  "responses": {\n' +
        '    "utter_welcome": [\n' +
        '      {\n' +
        '        "text": "您好！请问有什么可以帮到您的吗？"\n' +
        '      }\n' +
        '    ]\n' +
        '  },\n' +
        '  "session_config": {\n' +
        '    "session_expiration_time": 60,\n' +
        '    "carry_over_slots_to_new_session": true\n' +
        '  },\n' +
        '  "nlu": [\n' +
        '    {\n' +
        '      "intent": "welcome",\n' +
        '      "examples": [\n' +
        '        "你好呀",\n' +
        '        "你好啊",\n' +
        '        "你好"\n' +
        '      ]\n' +
        '    }\n' +
        '  ],\n' +
        '  "rules": null,\n' +
        '  "stories": [\n' +
        '    {\n' +
        '      "story": "financial_1",\n' +
        '      "steps": [\n' +
        '        {\n' +
        '          "intent": "welcome"\n' +
        '        },\n' +
        '        {\n' +
        '          "action": "utter_welcome"\n' +
        '        }\n' +
        '      ]\n' +
        '    }\n' +
        '  ]\n' +
        '}'
    var out=Process("plugins.rasa.get_rasa_yaml",s)
    return out
}

function X(){
    var s='test'
    var out=Process("plugins.rasa.get_rasa_yaml",s)
    return out
}
